<?php

namespace app\admin\controller\share;

use app\common\controller\Backend;
use think\Db;
use linghuo\Linghuo;


/**
 * 报单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * UserWithdraw模型对象
     * @var \app\admin\model\UserWithdraw
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

    }

    public function index(){
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            /*if ($this->request->request('keyField')) {
                return $this->selectpage();
            }*/

            $offset = $this->request->post("offset"); // 第几页
            $offset = empty( $offset) ? 0 :  bcsub($offset,1);
            $limit = $this->request->post("limit"); // 一页显示几行
            $limit = empty($limit) ? 10 :  $limit;
            $page = empty($offset) ? 0 :  bcmul($offset,$limit);

            $whereSql['shopro_order.goods_id'] = ['IN', [1,2,3,4,5,6]];
            $whereSql['shopro_order.status'] = 1;
            $whereSql['shopro_order.share_retreat'] = 0;

            $maps = [];
            $map = json_decode($this->request->get('filter'), true);
            if(!empty($map)){
                foreach ($map as $k=>$v){
                    if($k = 'username'){
                        $maps['user.username'] = $v;
                    }else{
                        $maps['shopro_order'.$k] = $v;
                    }
                }
            }

            $count = Db::table('shopro_order')->join('user', 'shopro_order.user_id=user.id', 'left')->where($whereSql)->where($maps)->count();

            $list = Db::table('shopro_order')->join('user', 'shopro_order.user_id=user.id', 'left')
                ->field('shopro_order.id,shopro_order.user_id,shopro_order.phone,shopro_order.total_amount,shopro_order.total_fee,shopro_order.createtime,user.username')
                ->where($whereSql)->where($maps)
                ->order('shopro_order.createtime', 'DESC')
                ->limit($page,$limit)->select();
            foreach($list as $k=>$v){
                if(preg_match("/^1[34578]{1}\d{9}$/",$v["phone"])){
                    $list[$k]["phone_xing"]=substr_replace($v["phone"], '****',3, 4);
                }else{
                    $list[$k]["phone_xing"]=$v["phone"];
                }
                if(preg_match("/^1[34578]{1}\d{9}$/",$v["username"])){
                    $list[$k]["username_xing"]=substr_replace($v["username"], '****',3, 4);
                }else{
                    $list[$k]["username_xing"]=$v["username"];
                }
            }

            $result = array("total" => $count, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();

    }

    public function set_goods_reward(){
        $time = date('Y-m-d');
        $startDay = date('Y-m-d', strtotime("$time -1 day"));
        $monthStart = strtotime($startDay);
        $monthEnd = strtotime(date('Y-m-d', strtotime("$startDay +1 day")));

        $info = Db::table('share_goods_reward')->where(['month'=>date('Y-m-d', $monthStart)])->find();
        if(!empty($info)){
            $this->error('昨天收益已生成，请勿重复操作');
        }

        $whereSql['goods_id'] = ['IN', [1,2,3,4,5,6]];
        $whereSql['status'] = 1;
        $whereSql['share_retreat'] = 0;
        $whereSql['createtime'] = ['between', [$monthStart, $monthEnd]];

        $count = Db::table('shopro_order')->where($whereSql)->count();
        $count = $count > 0 ? $count : 0;
        if($count <= 0){
            $this->error('昨日无订单信息！');
        }
        $userList = Db::table('share_user')->where(['is_union'=>1])->select();
        if(empty($userList)){
            $this->error('暂无联创伙伴用户！');
        }

        $amount = sprintf('%.2f', 100 * $count / count($userList));
        if(!empty($userList) && $amount > 0){
            foreach ($userList as $key=>$val){
//                $bankInfo = Db::table('shopro_user_bank')->where(['user_id'=>$val['user_id']])->find();
//                $data[$key]['real_name'] = isset($bankInfo['real_name']) ? $bankInfo['real_name'] : '';
//                $data[$key]['card_no'] = isset($bankInfo['card_no']) ? $bankInfo['card_no'] : '';
//                $data[$key]['bank_name'] = isset($bankInfo['bank_name']) ? $bankInfo['bank_name'] : '';
                $data[$key]['user_id'] = $val['user_id'];
                $data[$key]['money'] = $amount;
                $data[$key]['status'] = 1;
                $data[$key]['month'] = date('Y-m-d', $monthStart);
                $data[$key]['addtime'] = time();
            }
            $res = Db::table('share_goods_reward')->insertAll($data);
            if($res){
                $this->success('操作成功');
            }
        }
        $this->error('操作失败');
    }

}
